package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.bo.ExpertDiagnosisSummaryBO;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.common.enums.OnlineBizStatusEnum;
import com.pshcp.domain.dto.OnlineSessionDTO;
import com.pshcp.domain.model.OnlineSession;
import com.pshcp.mapper.OnlineSessionMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 在线咨询会话 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-12-10
 */
@Service
public class OnlineSessionService {

    @Autowired
    private OnlineSessionMapper onlineSessionMapper;

    // 查询需要关闭的数据
    public List<OnlineSession> queryOnlineSessionNeedAutoClose(){
        return onlineSessionMapper.queryOnlineSessionNeedAutoClose();
    }

    // 修改咨询状态
    public void updateOnlineSessionStatus(Long id, Integer sessionStatus){
        onlineSessionMapper.updateOnlineSessionStatus(id, sessionStatus);
    }

    // 查询问题未回复数据
    public List<OnlineSession> queryOnlineSessionNoReply(){
        return onlineSessionMapper.queryOnlineSessionNoReply();
    }

    // 修改提醒次数
    public void updateOnlineSessionRemindNum(Long id){
        onlineSessionMapper.updateOnlineSessionRemindNum(id);
    }

    // 修改问题是否回复
    public void updateOnlineSessionIsReply(Long patientOnlineOrderId){
        onlineSessionMapper.updateOnlineSessionIsReply(patientOnlineOrderId);
    }

    public OnlineSession getOnlineSessionByPatientOnlineOrderId(Long patientOnlineOrderId) {
        Example example = new Example(OnlineSession.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("patientOnlineOrderId", patientOnlineOrderId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        List<OnlineSession> list = onlineSessionMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public OnlineSession getOnlineSessionBySessionId(String sessionId) {
        Example example = new Example(OnlineSession.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("sessionId", sessionId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        List<OnlineSession> list = onlineSessionMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 0 医生
     * 1 患者
     */
    public List<OnlineSession> getOnlineSessionList(Integer type, Integer userType, Long userId, List<Integer> sessionStatusList) {
        Example example = new Example(OnlineSession.class);
        Example.Criteria criteria = example.createCriteria();
        if (userType == 0) {
            criteria.andEqualTo("doctorId", userId);
            criteria.andEqualTo("doctorDelStatus", CommonStatusEnum.INIT.getCode());
        }
        if (userType == 1) {
            criteria.andEqualTo("patientUserId", userId);
            criteria.andEqualTo("patientDelStatus", CommonStatusEnum.INIT.getCode());
        }
        if (type != null) {
            criteria.andEqualTo("type", type);
        }
        criteria.andNotIn("sessionStatus", sessionStatusList);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" session_status asc");
        List<OnlineSession> list = onlineSessionMapper.selectByExample(example);
        return list;
    }

    /**
     * 退诊
     *
     * @param onlineSessionDTO
     */
    public void refundOnlineSession(OnlineSessionDTO onlineSessionDTO) {
        Example example = new Example(OnlineSession.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("patientOnlineOrderId", onlineSessionDTO.getPatientOnlineOrderId());

        OnlineSession onlineSession = new OnlineSession();
        onlineSession.setSessionStatus(OnlineBizStatusEnum.REFUND.getCode());
        onlineSessionMapper.updateByExampleSelective(onlineSession, example);
    }

    public PageList<OnlineSession> list(OnlineSessionDTO onlineSessionDTO) {
        if (onlineSessionDTO.getPageNum() != null && onlineSessionDTO.getPageSize() != null) {
            PageHelper.startPage(onlineSessionDTO.getPageNum(), onlineSessionDTO.getPageSize());
        }
        Example example = new Example(OnlineSession.class);
        Example.Criteria criteria = example.createCriteria();
        if (onlineSessionDTO.getStatus() != null) {
            criteria.andEqualTo("status", onlineSessionDTO.getStatus());
        } else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if (!CollectionUtils.isEmpty(onlineSessionDTO.getIds())){
            criteria.andIn("id", onlineSessionDTO.getIds());
        }
        if (onlineSessionDTO.getType() != null) {
            criteria.andEqualTo("type", onlineSessionDTO.getType());
        }
        if (onlineSessionDTO.getSource() == 0) {
            criteria.andEqualTo("doctorId", onlineSessionDTO.getDoctorId());
            criteria.andEqualTo("doctorDelStatus", CommonStatusEnum.INIT.getCode());
            if (onlineSessionDTO.getPatientId() != null) {
                criteria.andEqualTo("patientId", onlineSessionDTO.getPatientId());
            }
        }
        if (onlineSessionDTO.getSource() == 1) {
            criteria.andEqualTo("patientUserId", onlineSessionDTO.getPatientUserId());
            criteria.andEqualTo("patientDelStatus", CommonStatusEnum.INIT.getCode());
        }
        if (!CollectionUtils.isEmpty(onlineSessionDTO.getSessionStatusList())) {
            criteria.andIn("sessionStatus", onlineSessionDTO.getSessionStatusList());
        }
        //搜素
        if (!StringUtils.isBlank(onlineSessionDTO.getSearch())) {
            Example.Criteria criteria1 = example.createCriteria();
            criteria1.orLike("patientName", "%" + onlineSessionDTO.getSearch() + "%");
            example.and(criteria1);
        }
        example.setOrderByClause(" update_time desc");
        List<OnlineSession> list = onlineSessionMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<OnlineSession> select(OnlineSession onlineSession) {
        return onlineSessionMapper.select(onlineSession);
    }

    public Long insert(OnlineSession onlineSession) {
        onlineSession.setStatus(CommonStatusEnum.INIT.getCode());
        onlineSession.setCreatedTime(new Date());
        onlineSessionMapper.insert(onlineSession);
        return onlineSession.getId();
    }

    public int update(OnlineSession onlineSession) {
        onlineSession.setUpdateTime(new Date());
        return onlineSessionMapper.updateByPrimaryKeySelective(onlineSession);
    }

    public OnlineSession findById(Long id) {
        return onlineSessionMapper.selectByPrimaryKey(id);
    }

    public List<OnlineSession> findByIds(List<Long> ids) {
        Example example = new Example(OnlineSession.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return onlineSessionMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(OnlineSession.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        OnlineSession onlineSession = new OnlineSession();
        onlineSession.setStatus(CommonStatusEnum.DEL.getCode());
        return onlineSessionMapper.updateByExampleSelective(onlineSession, example);
    }

    public Integer getOnlineSessionCount2Wzl(Long doctorId,String startTime,String endTime) {
        Example example = new Example(OnlineSession.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andEqualTo("type", 0);
        criteria.andGreaterThanOrEqualTo("createdTime", startTime+" 00:00:00");
        criteria.andLessThanOrEqualTo("createdTime", endTime+" 23:59:59");
        return onlineSessionMapper.selectCountByExample(example);
    }

    public List<ExpertDiagnosisSummaryBO> getExpertDiagnosisSummaryList(OnlineSessionDTO onlineSessionDTO) {
        return onlineSessionMapper.getExpertDiagnosisSummaryList(onlineSessionDTO);
    }
}
